<?php
/**
 * 门店管理
 */

namespace app\badmin\controller;


use app\badmin\model\Company;
use app\badmin\model\CompanyAccount;
use app\badmin\model\Region;
use app\common\controller\BadminController;
use app\common\lib\RespCode;
use custom\Err;
use think\Exception;
use app\badmin\model\CompanyShop as CompanyShopModel;
use think\facade\Request;

class CompanyShop extends BadminController {

    //门店禁止访问
    protected $banShop = ['*'];

    public $resubmitLock = ['addpost', 'editpost'];

    /**
     * 添加提交
     * @return false|string
     */
    public function addPost() {
        try {
            $params = $this->checkParams('name,phone,pwd,pwd_again,address,agent_num,city_code,lat,lng');
            if ($params === false) return $this->resErr();
            $res = CompanyShopModel::add($params);
            if (!$res) return $this->resErr();
            else return $this->resData('创建成功', []);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 添加获取
     * @return false|string
     */
    public function addGet() {
        try {
            $params = $this->checkParams('');
            if ($params === false) return $this->resErr();
            $citys = Company::where('id', $params['company_id'])->value('city_code');
            $citys = explode(',', $citys);//可选城市数据
            foreach ($citys as $k => $v) {
                $res[$k]['key'] = $v;
            }
            $laveNum = CompanyAccount::getCompanyLaveNum($params['company_id']);//公司剩余可添加剩余经纪人数
            return $this->resData('success', compact('res', 'laveNum'));
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 门店列表
     * @return false|string
     */
    public function getList() {
        try {
            $params = $this->checkParams('');
            if ($params === false) return $this->resErr();
            $list = CompanyShopModel::getList($params);
            return $this->resData('success', $list);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 编辑获取
     * @return false|string
     */
    public function editGet() {
        try {
            $params = $this->checkParams('id');
            if ($params === false) return $this->resErr();
            $info = CompanyShopModel::getInfo($params);
            if (!$info) return $this->resErr();
            $citys = Company::where('id', $params['company_id'])->value('city_code');
            $citys = explode(',', $citys);//可选城市数据
            foreach ($citys as $k => $v) {
                $res[$k]['key'] = $v;
            }
            $laveNum = CompanyAccount::getCompanyLaveNum($params['company_id']);//公司剩余可添加剩余经纪人数
            $laveNum['lave_num'] = $laveNum['lave_num'] + $info['agent_num'];
            //最低不能低于门店已使用人数
            $bottom_line = $info['agent_num'] - $info['lave_num'];
            if ($bottom_line < 1) $bottom_line = 1;
            return $this->resData('success', compact('info', 'res', 'laveNum', 'bottom_line'));
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 编辑提交
     * @return false|string
     */
    public function editPost() {
        try {
            $params = $this->checkParams('id,name,address,agent_num,city_code,lat,lng');
            if ($params === false) return $this->resErr();
            $res = CompanyShopModel::edit($params);
            if (!$res) return $this->resErr();
            else return $this->resData('编辑成功', []);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 删除
     * @return false|string
     */
    public function del() {
        try {
            $params = $this->checkParams('id');
            if ($params === false) return $this->resErr();
            $re = CompanyShopModel::del($params);
            if (!$re) return $this->resErr();
            else return $this->resData('删除成功', []);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

}